import 'package:flutter/material.dart';
import 'package:learning/bean/learning_type_entity.dart';
import 'package:learning/components/MyText.dart';
import 'package:learning/service/CategoryService.dart';

import '../../IconFontIcons.dart';
import '../../components/CustomBar.dart';
import '../../generated/l10n.dart';

class CategoryPage extends StatefulWidget {
  final Object? arguments;

  const CategoryPage({super.key, this.arguments});

  @override
  _CategoryPageState createState() => _CategoryPageState();
}

class _CategoryPageState extends State<CategoryPage> {
  late Future<List<LearningTypeEntity>> futureData;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    futureData = fetchTodo();
  }

  Future<List<LearningTypeEntity>> fetchTodo() async {
    //listData=[];

    List<LearningTypeEntity> listData =
        await CategoryService.getLearningTypeList();
    if (listData.length > 0) {
      return listData;
    } else {
      throw Exception('Failed to load data');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomBar(back: true, title: S.of(context).monikaoshi),
        body: Container(
            color: Colors.black.withAlpha(12),
            child: FutureBuilder<List<LearningTypeEntity>>(
                future: futureData,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return ListView.builder(
                        itemCount: snapshot.data!.length,
                        itemBuilder: (context, index) {
                          return getMoreItem(snapshot.data![index]);
                        });
                  } else if (snapshot.hasError) {
                    return Text('${snapshot.error}');
                  } else {
                    return const Center(child: CircularProgressIndicator());
                  }
                })));
  }

  Widget getMoreItem(LearningTypeEntity data) {
    return InkWell(
        child: Container(
            color: Colors.white,
            margin: EdgeInsets.only(top: 5, bottom: 5),
            padding: EdgeInsets.all(10),
            child: Row(
              children: [
                Expanded(child: MyText(data.name)),
                Container(
                    width: 50,
                    child: Icon(IconFontIcons.iconArrowLeft2,
                        color: Colors.black, size: 15)),
              ],
            )));
  }
}
